<?php
session_start(); //we need to call PHP's session object to access it through CI
class Ujian extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->library('view');
		$this->view->set_menu('ujian');
		$this->view->set_portlet_title('List Ujian');
		$this->view->set_portlet_style('portlet box blue');
		$this->view->set_view('siswa/siswa_ujian');
		$this->session_data = $this->session->userdata('user');
	}

	function index()
	{ 	
		date_default_timezone_set('Asia/Pontianak');
		$this->load();
		$this->load->model('m_kelas_ujian');
		$this->data['title'] = 'Home';
		$uj_id = $this->m_kelas_ujian->get_ujian_with_id($this->data['id']);
		$rows = $this->m_kelas_ujian->get_ujian_by_kelas($this->data['username'], count($uj_id)?$uj_id:null);
		$i = 0;
		foreach ($rows as $row){
			$from = $row->uj_date.' '.$row->uj_start;
			$to = $row->uj_date.' '.$row->uj_end;
			$rows[$i]->start = $this->date_is_between($from, $to);
			$i++;
		}
		$this->data['ujian'] = $rows;
		$this->view->show($this->data);
	}
	
	function date_is_between($from, $to, $date = 'now') {
		$date = is_int($date) ? $date : strtotime($date); // convert non timestamps
		$from = is_int($from) ? $from : strtotime($from); // ..
		$to = is_int($to) ? $to : strtotime($to);         // ..
		if(($date >= $from) && ($date <= $to)) return 1;
		else if($date <= $from) return 2;
		else return 0; // extra parens for clarity
	}
	
	function is_already($id){
		$this->load->model('m_ujian');
		$ujian = $this->m_ujian->count_ujian_by_swid($id, $this->data['id']);
		if($ujian > 0) return true;
	}
	
	function is_over($ujian){
		if(strtotime($ujian->uj_date." ".$ujian->uj_end) < strtotime(date('d-m-Y H:i:s'))) return True;
		else return False;
	}
	
	function masuk_ujian($id){
		$this->load();			
		if($this->is_already($id)){
			$this->data['error'] = "Ujian sudah diikuti, berikut hasilnya ".$id;
			$this->show_result($id);
		}else{
			$this->data['title'] = 'Home';
			$this->data['uj_id'] = $id;
			$ujian = $this->m_ujian->get_ujian_with_soal($id);

			if($this->is_over($ujian[0])){
				$this->data['error'] = "Ujian sudah berakhir";
				$this->submit_ujian($id);
				$this->show_result($id);
				return;
			}

			$this->data['ujian'] = $this->build_quiz($ujian);
			$this->data['end_time'] = $ujian[0]->uj_date.' '.$ujian[0]->uj_end;
			$this->view->set_menu('ujian');
			$this->view->set_portlet_title($ujian[0]->uj_name.' - '.$ujian[0]->mp_name);
			$this->view->set_portlet_style('portlet box yellow');
			$this->view->set_view('siswa/siswa_quiz');	
			$this->view->show($this->data);
		}
	}
	
	function build_quiz($quizs){
		$i = 0;
		foreach($quizs as $quiz){
			$soal[$quiz->so_id]['pertanyaan'] = $quiz;
			$soal[$quiz->so_id]['jawaban'][] = $quiz;
			if($i % $quiz->uj_n_jawaban == ($quiz->uj_n_jawaban-1)){
				shuffle($soal[$quiz->so_id]['jawaban']);
			}
			$i++;
		}
		shuffle($soal);
		return $soal;
	}
	
	function submit_ujian($id=null){
		$this->load->model('m_nilai');
		if(!isset($_POST['uj_id'])){
			$value['uj_id'] = $id;
			$value['sw_id'] = $this->session_data['id'];
			$value['nilai'] = 0;
			$value['soal'] = '';
			$value['jawaban'] = '';
		}else{
			$soal = $this->input->post('soal');
			$value['uj_id'] = $this->input->post('uj_id');
			$value['sw_id'] = $this->session_data['id'];
			$value['soal'] = '';
			$value['jawaban'] = '';
			$right = 0;
			$i = 0;
			foreach ($soal as $row){
				if($i){
					$value['soal'] .= ',';
					$value['jawaban'] .= ',';				
				}
				$value['soal'] .= $row;
				$jawaban = explode('|', $this->input->post('jawaban'.$row));
				$value['jawaban'] .= $jawaban[0];
				if($jawaban[1] == '1') $right += 1;
				$i++;
			}
			$value['nilai'] = $this->calculate(count($soal), $right);
		}
		$this->m_nilai->insert($value);
		//$this->show_result($value['uj_id']);
		echo $value['uj_id'];
	}
	
	function calculate($soal, $value){
		$nilai = $value / $soal * 100;
		return $nilai;
	}
	
	function show_result($id){
		$this->load();			
		$this->load->model('m_nilai');
		$this->load->model('m_ujian');
		$this->load->model('m_jawaban');
		$rows = $this->m_nilai->get_data(array('uj_id' => $id, 'sw_id' => $this->session_data['id']));

		$this->data['soal'] = $this->m_ujian->get_ujian_with_soal($id, array('jawaban.ja_stat = "1"'));
		$this->data['title'] = 'Hasil Ujian';
		$this->data['nilai'] = $rows[0]->nilai;
		$this->data['jawaban'] = array();
		$this->data['return'] = 'user/laporan';

		$soal = explode(',', $rows[0]->soal);
		asort($soal);
		$ja = explode(',', $rows[0]->jawaban);
		$jawaban = $this->m_jawaban->get_data_in($ja);
		$i = 0;
		if(count($jawaban)):
			foreach ($soal as $row){
				$this->data['jawaban'][$row]['desc'] = $jawaban[$i]['ja_desc'];
				$this->data['jawaban'][$row]['stat'] = $jawaban[$i]['ja_stat'];
				$i++;
			}
		endif;
		$this->view->set_menu('ujian');
		$this->view->set_portlet_title('Hasil '.$this->data['soal'][0]->uj_name.' - '.$this->data['soal'][0]->mp_name);
		$this->view->set_portlet_style('portlet box green');
		$this->view->set_view('siswa/siswa_result');
		$this->view->show($this->data);
	}
	
	function load(){
		if($this->session_data){
			$this->data['id'] = $this->session_data['id'];
			$this->data['username'] = $this->session_data['username'];
			$this->data['nama'] = $this->session_data['nama'];
		}else{
			redirect('user/login', 'refresh');
		}
	}
}
?>